package com.xxxx.crm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxx.crm.base.BaseService;
import com.xxxx.crm.dao.CusDevPlanMapper;
import com.xxxx.crm.dao.SaleChanceMapper;
import com.xxxx.crm.query.CusDevPlanQuery;
import com.xxxx.crm.utils.AssertUtil;
import com.xxxx.crm.vo.CusDevPlan;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:王亚栋
 * @Date:2022/12/25 19:07
 * @Version:1.0
 */
@Service
public class CusDevPlanService extends BaseService<CusDevPlan,Integer> {

    @Autowired
    private CusDevPlanMapper cusDevPlanMapper;
    @Autowired
    private SaleChanceMapper saleChanceMapper;

    /**
     * 多条件查询客户开发机计划
     * @param cusDevPlanQuery
     * @return
     */
    public Map<String,Object> queryCusDevPlanByParams(CusDevPlanQuery cusDevPlanQuery){

        Map<String,Object> map = new HashMap<>();

        //开启分页
        PageHelper.startPage(cusDevPlanQuery.getPage(),cusDevPlanQuery.getLimit());
        //得到对应分页对象
        PageInfo<CusDevPlan> pageInfo = new PageInfo<>(cusDevPlanMapper.selectByParams(cusDevPlanQuery));

        //设置map对象
        map.put("code",0);
        map.put("msg","success");
        map.put("count",pageInfo.getTotal());
        //设置分页好的列表
        map.put("data",pageInfo.getList());

        return map;

    }


    /**
     * 添加客户开发计划
     *      参数校验
     *          营销机会ID   非空
     *          计划项内容   非空
     *          计划时间     非空
     *      设置参数默认值
     *          是否有效    默认有效
     *          创建时间    系统当前时间
     *          修改时间    系统当前时间
     *      执行添加操作，判断受影响行数
     * @param cusDevPlan
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void addCusDevPlan(CusDevPlan cusDevPlan){

        //参数校验
        checkCusDevPlanParams(cusDevPlan.getSaleChanceId(),cusDevPlan.getPlanDate(),cusDevPlan.getPlanItem());

        //设置默认值
        cusDevPlan.setIsValid(1);
        cusDevPlan.setUpdateDate(new Date());
        cusDevPlan.setCreateDate(new Date());
        //执行添加操作
        AssertUtil.isTrue(cusDevPlanMapper.insertSelective(cusDevPlan) != 1,"计划项数据添加失败！");

    }

    /**
     * 参数校验
     * @param saleChanceId
     * @param planDate
     * @param planItem
     */
    private void checkCusDevPlanParams(Integer saleChanceId, Date planDate, String planItem) {

        AssertUtil.isTrue(null == saleChanceId || saleChanceMapper.selectByPrimaryKey(saleChanceId) == null,"数据异常，请重试！");

        AssertUtil.isTrue(StringUtils.isBlank(planItem),"计划内容不能为空！");

        AssertUtil.isTrue(null == planDate,"计划时间不能为空！");
    }

    /**
     * 更新客户开发计划
     *      1.参数校验
     *      计划项ID       非空，数据存在
     *      营销机会ID      非空，数据存在
     *      计划项内容       非空
     *      计划时间        非空
     *     2.设置默认值参数
     *      修改时间        系统当前时间
     *     3.执行更新操作，判断受影响行数
     * @param cusDevPlan
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateCusDevPlan(CusDevPlan cusDevPlan){

        //参数校验
        AssertUtil.isTrue(null == cusDevPlan.getId() || cusDevPlanMapper.selectByPrimaryKey(cusDevPlan.getId()) == null,"数据异常，请重试！");

        checkCusDevPlanParams(cusDevPlan.getSaleChanceId(),cusDevPlan.getPlanDate(),cusDevPlan.getPlanItem());

        //修改默认值
        cusDevPlan.setUpdateDate(new Date());

        //执行修改操作
        AssertUtil.isTrue(cusDevPlanMapper.updateByPrimaryKeySelective(cusDevPlan) != 1,"计划项更新失败！");

    }

    /**
     * 删除计划项数据
     * @param id
     */
    public void deleteCusDevPlan(Integer id) {

        //判断id数据是否存在
        AssertUtil.isTrue(null == id,"待删除数据不存在，请重新输入！");
        //通过ID查询计划项对象
        CusDevPlan cusDevPlan = cusDevPlanMapper.selectByPrimaryKey(id);
        //设置记录无效
        cusDevPlan.setIsValid(0);
        cusDevPlan.setUpdateDate(new Date());

        //执行删除操作
        AssertUtil.isTrue(cusDevPlanMapper.updateByPrimaryKeySelective(cusDevPlan) != 1,"计划项数据删除失败！");

    }
}
